/*******************************************************************************
 * copyright (c) 2009 - 2010 Simon Allen
 *
 * This file is part of Convex Media Converter.
 *
 * Convex Media Converter is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Convex Media Converter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Convex Media Converter; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 ******************************************************************************/


#ifndef CONVERTDIALOG_H
#define CONVERTDIALOG_H

#ifdef unix
	#include <libnotify/notify.h>
#endif

#include "../OutputSettings/outputsettings.h"
#include "../FileProperties/fileproperties.h"
#include "ui_convertdialog.h"

#include <QWidget>
#include <qprocess.h>


const int SECONDS_IN_MIN = 60, SECONDS_IN_HOUR = 3600;
const QString FFMPEG_AUDIO_CODEC = "-acodec";
const QString FFMPEG_AUDIO_BITRATE = "-ab";
const QString FFMPEG_AUDIO_SAMPLE_RATE = "-ar";
const QString FFMPEG_AUDIO_CHANNELS = "-ac";
const QString FFMPEG_AUDIO_QUALITY = "-aq";
const QString FFMPEG_DISABLE_AUDIO = "-an";
const QString FFMPEG_VIDEO_CODEC = "-vcodec";
const QString FFMPEG_VIDEO_BITRATE = "-b";
const QString FFMPEG_VIDEO_SIZE = "-s";
const QString FFMPEG_VIDEO_ASPECT = "-aspect";
const QString FFMPEG_FRAME_RATE = "-r";
const QString FFMPEG_DISABLE_VIDEO = "-vn";
const QString FFMPEG_MAP_METADATA = "-map_meta_data";


class ConvertDialog : public QDialog
{
	Q_OBJECT

	private:
		Ui::ConvertDialog *ui;
		QTextEdit *textEdit_details;
		QProcess *ffmpeg;
		QString FFmpegLocation;
		QString currentOutputFile;
		QString outputDir;
		QString command;
		enum {ASK, DELETE, PRESERVE} fileExistsAction;
		int filesDone;
		long duration;
		long totalDuration;
		OutputSettings outputSettings;
		QList<QFileInfo> inputFiles;
		QList<QFileInfo>::iterator currentInputFileIter;
		PresetSettings *settings;
		NotifyNotification* notification;

	public:
		ConvertDialog(PresetSettings *newSettings, QWidget *parent = NULL);
		~ConvertDialog();
		QStringList getConversionArgs();
		int countProcs() const;
		void convert(const QString newFFmpegLocation, const QList<QFileInfo> files,
			     const OutputSettings newSettings);
		void convertNextFile();
		void shutdownComputer();
		void showFinishedNotification() const;
		void deleteFile(const QString outputFile) const;
		void clear();

	public slots:
		void readFromStdout();
		void detailsToggled(const bool on);
		void fileConverted(int exitCode, QProcess::ExitStatus exitStatus);
		void finishConversion(QAbstractButton *button);
};

#endif // CONVERTDIALOG_H
